home *** CD-ROM | disk | FTP | other *** search
- /****************************************************************************/
- /* */
- /* Module: fdump.c - Display contents of a file */
- /* */
- /* Programmer: George R. Woodside */
- /* */
- /* Date: January 8, 1987 */
- /* */
- /* Function: Display the contents of a file in hex ans ASCII */
- /* */
- /****************************************************************************/
-
- #include <stdio.h>
-
-
- main(argc,argv)
- int argc;
- char *argv[];
-
- {
-
- int i;
- FILE *fp;
-
- if (argc > 1) /* if multiple arguments, */
- {
- for (i = 1; i < argc; i++) /* then do each one */
- {
- printf("******************************\n"); /* set up header */
- printf("%s\n", argv[i]); /* print file name */
- printf("******************************\n");
- if( (fp = fopenb(argv[i],"r") ) == NULL) /* if error opening file, */
- {
- fprintf(stderr,"%s: Unable to open %s \n",argv[0],argv[i]);
- continue;
- }
- else /* if file opened OK, */
- {
- dump(fp); /* print it */
- printf("\f\n"); /* tack on a form feed */
- } /* end file opened OK */
- } /* end for one argument */
- } /* end for all arguments */
- else /* if no arguments, */
- dump(stdin); /* dump standard in */
- } /* end main */
-
- /****************************************************************************/
- /* dump - hex dump */
- /****************************************************************************/
- dump(fp)
- FILE *fp; /* file to dump */
-
- {
- static char data[] = " "; /* build ASCII image here */
- int i = 0;
- long count = 0L;
- int ch;
-
- while(( ch=fgetc(fp)) != EOF) /* for each character, */
- {
- if(i == 0) /* if at the beginning of a line, */
- printf("%06X ",count); /* print the byte address */
-
- printf("%02x ",(int) (ch & 0xff) ); /* then the byte */
-
- if(i == 7)
- printf(" "); /* stick extra break in center */
-
- if( ( ch > 0x1f) && (ch < 0x7f) ) /* if character is printable, */
- data[i++] = ch; /* use it. */
- else /* if not printable, */
- data[i++] = '.'; /* use a period */
-
- if(i == 16) /* if a full line is done, */
- {
- printf(" %s \n",data); /* print the ASCII version, */
- i = 0; /* reset the line count */
- count += 16; /* and increment the byte address */
- } /* end of one line */
- } /* end of file */
- while (i < 16) /* if not multiple of 16 bytes */
- {
- printf(" "); /* space over */
- data[i++] = ' '; /* align ASCII portion of line */
- }
- printf(" %s \n",data); /* print ASCII for last line */
-
- fclose(fp); /* close current file */
- }
-